package com.lcb.jmm.synchronizeddemo;

import com.lcb.jmm.volatiledome.UnsafeInstance;

/****
 * @Author:lcb
 * @Description:Synchronized底层分析
 * @Date: 2020-12-24 15:24
 *****/
public class SynchronizedDemo {

    private static Object object = new Object();


    public static void lock() {
//        synchronized (object) {
//            System.out.println("测试哦");
//        }
        UnsafeInstance.reflectGetUnsafe().monitorEnter(object);
    }

    public static void tes1t() {
        UnsafeInstance.reflectGetUnsafe().monitorExit(object);
    }

    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer();
        synchronized (object) {
            sb.append("a");
        }
        synchronized (object) {
            sb.append("b");
        }
        synchronized (object) {
            sb.append("c");
        }
        //优化完  jvm优化 锁粗化
        synchronized (object) {
            sb.append("a");
            sb.append("b");
            sb.append("c");
        }


    }

    public void test() {
        /**
         * 锁的消除
         */
        synchronized (new Object()) {
            StringBuffer sb = new StringBuffer();
            sb.append("a");
            sb.append("b");
            sb.append("c");
        }
    }
}
